﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

namespace VFramework{
	/// <summary>
	/// 滚动数字类
	/// 挂载在对象UI.text上
	/// </summary>
	public class VUIScrollLabel : MonoBehaviour
	{

		private int value;
		private int value2;

		void Start ()
		{
			
		}

		void Update ()
		{
			if (value == value2) {
				return;
			}
			int diff = Mathf.Abs (value2 - value);
			int sign = (value2 - value) / diff;
			int step = 10;
			if (diff > 2000) {
				step = 1000;
			} else if (diff > 1000)
				step = 500;
			else if (diff > 500)
				step = 200;
			else if (diff > 200)
				step = 35;
			else if (diff > 100)
				step = 20;
			
			
			if (diff < step)
				step = diff;

			value += step * sign;
			setText (value + "");
		}

		public void setValue (int value)
		{
			this.value = value;
			this.value2 = value;
			setText (value + "");
		}

		public int getValue ()
		{
			return this.value2;
		}

		public void setValueWithAnimation (int value)
		{
			this.value2 = value;
		}

		public void setOffsetValueWithAnimation (int value)
		{
			this.value2 += value;
		}

		void setText (string text)
		{
			GetComponent<Text> ().text = text;
		}
	}
}